package org.song.controller;

import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.song.exception.result.AjaxResult;
import org.song.model.dto.ViewDto;
import org.song.model.vo.View;
import org.song.service.ViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 博客访问记录控制层
 */
@Api(tags = "博客访问记录控制层")
@Controller
@RequestMapping(value = "/view")
public class ViewController {

    @Autowired( required = true )
    @Qualifier( value = "viewServiceImpl")
    private ViewService viewService;

    /**
     * 查询所有的博客访问记录
     * @param viewDto
     * @return
     */
    @ApiOperation(value = "查询所有的博客访问记录" , notes = "查询所有的博客访问记录")
    @ResponseBody
    @RequestMapping(value = "/listView" , method = RequestMethod.GET)
    public AjaxResult<List<View>> listView(@ApiParam(value = "viewDto实例接受查询参数") @NonNull ViewDto viewDto) {
        PageHelper.startPage(viewDto.getPage() , viewDto.getSize());
       return AjaxResult.success(viewService.listView(viewDto));
    }

    /**
     * 删除博客访问记录
     * @param view
     * @return
     */
    @ApiOperation(value = "删除博客访问记录" , notes = "删除博客访问记录")
    @ResponseBody
    @RequestMapping(value = "/deleteView" , method = RequestMethod.DELETE)
    public AjaxResult<Integer> deleteView(@ApiParam(value = "接受需要删除的博客访问记录") @NonNull @RequestBody View view) {
        return AjaxResult.success(viewService.deleteView(view));
    }

    /**
     * 增加博客访问记录
     * @param view
     * @return
     */
    @ApiOperation(value = "增加博客访问记录" , notes = "增加博客访问记录")
    @ResponseBody
    @RequestMapping(value = "/insertView" , method = RequestMethod.PUT)
    public AjaxResult<Integer> insertView(@ApiParam(value = "博客访问记录") @NonNull @RequestBody View view) {
        return AjaxResult.success(viewService.insertView(view));
    }

    /**
     * 更新博客访问记录
     * @param view
     * @return
     */
    @ApiOperation(value = "更新博客访问记录" , notes = "更新博客访问记录")
    @ResponseBody
    @RequestMapping(value = "/updateView" , method = RequestMethod.POST)
    public AjaxResult<Integer> updateView(@ApiParam(value = "根据传入的访问记录信息进行博客访问记录更新") @NonNull @RequestBody View view) {
        return AjaxResult.success(viewService.updateView(view));
    }


}
